package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.OrderDetailet;
import com.foreknow.elm.po.Orders;

import com.foreknow.elm.util.CommonUtil;

import com.foreknow.elm.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;
    //根据用户编号、商家编号、订单总金额、送货地址编号向订单表中添加一条记录，封装了一个订单对象,返回订单编号(主键)
    @Override
    public int insert(Orders orders) throws SQLException {
        //a为返回的主键
        int a = 0;
        String sql = "insert into orders (userId,businessId,orderDate,daId,orderTotal) values (?,?,?,?,?)";
        try {
            conn = DBUtil.getConnection();
            //订购日期需要主动生成
            String date = CommonUtil.getCurrentDate();
            //获取自动生成的主键Statement.RETURN_GENERATED_KEYS
            pst = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            String userid = orders.getUserId();
            Integer bid = orders.getBusinessId();
            Integer daid = orders.getDaId();
            double total = orders.getOrderTotal();
            pst.setString(1,userid);
            pst.setInt(2,bid);
            pst.setString(3,date);
            pst.setInt(4,daid);
            pst.setDouble(5,total);
            //没接返回行数
            pst.executeUpdate();
            //添加成功返回主键
                rs = pst.getGeneratedKeys();
            if (rs.next()){
                a = rs.getInt(1);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return a;
    }
//根据订单编号查询订单信息，包括所属商家信息，和此订单的所有订单明细信息
    @Override
    public Orders queryobo(Integer orderId) throws SQLException {
        Orders orders = new Orders();
        String sql = "select * from orders o,business b,orderdetailet l where o.businessId = b.businessId and o.orderId = l.orderId and o.orderId=?";
        //订单,订单明细,商家 三表连接
        try {
            conn = DBUtil.getConnection();
            pst = conn.prepareStatement(sql);
            pst.setInt(1,orderId);
//            List<OrderDetailet> list = new ArrayList<>();
            rs = pst.executeQuery();
            while (rs.next()){
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));
                //添加business
                Business business = new Business();
                business.setBusinessId(Integer.parseInt(rs.getString("businessId")));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(Integer.parseInt(rs.getString("orderTypeId")));
                business.setStarPrice(Double.valueOf(rs.getString("starPrice")));
                business.setDeliveryPrice(Double.valueOf(rs.getString("deliveryPrice")));
                business.setRemarks(rs.getString("remarks"));
                orders.setBusiness(business);
                //订单明细一对多在service中查询set
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return orders;
    }
//根据用户编号查询此用户的所有订单信息
    @Override
    public List<Orders> queryall(String userId) throws SQLException {
        List<Orders> list = new ArrayList<>();
        String sql = "select * from orders o,business b,orderdetailet l where o.businessId = b.businessId and o.orderId = l.orderId and o.userId=?";
        try {
            //订单,订单明细,商家 三表连接
            conn = DBUtil.getConnection();
            pst = conn.prepareStatement(sql);
            pst.setString(1,userId);
            rs = pst.executeQuery();
            while (rs.next()){
                Orders orders = new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));
                //添加business
                Business business = new Business();
                business.setBusinessId(rs.getInt("b.businessId"));
                business.setBusinessName(rs.getString("b.businessName"));
                business.setBusinessAddress(rs.getString("b.businessAddress"));
                business.setBusinessExplain(rs.getString("b.businessExplain"));
                business.setBusinessImg(rs.getString("b.businessImg"));
                business.setOrderTypeId(rs.getInt("b.orderTypeId"));
                business.setStarPrice(rs.getDouble("b.starPrice"));
                business.setDeliveryPrice(rs.getDouble("b.deliveryPrice"));
                business.setRemarks(rs.getString("b.remarks"));
                orders.setBusiness(business);
                list.add(orders);
                //订单明细一对多在service中查询set
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }
//根据orderid修改支付状态
    @Override
    public int pay(Integer orderId) throws SQLException {
        int a = 0;
        String sql = "update orders set orderState = 1 where orderId = ?";
        try {
            conn = DBUtil.getConnection();
            pst = conn.prepareStatement(sql);
            pst.setInt(1,orderId);
            a = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return a;
    }
}

